//
//  DeviceListViewController.m
//  Lightthings
//
//  Created by Hero on 2024/11/22.
//

#import "DeviceListViewController.h"
#import "HomeDeviceViewModel.h"
@interface DeviceListViewController ()

@end

@implementation DeviceListViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self initData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.jy_title = SWString(@"设备列表");

    self.collectionInsets = kEdge(self.jy_bar_h, 0, klSafeAreaEdgeInsets.bottom, 0);
    
//    [self initData];
    // Do any additional setup after loading the view.
}
-(BOOL)can_pulldown {return NO;}
- (void)initData {
    
    MainSectionModel * s = [[MainSectionModel alloc] init];
    [s sectionInsets:kEdge(10, 10, 10, 10) ls:10 is:10];
    
    NSArray< CBPeripheral *> * arr = [FRQBluetoothManager shareManager].curPeripherals;
    for (int i = 0; i < arr.count; i ++) {
        HomeDeviceViewModel * m = [[HomeDeviceViewModel alloc] init];
        m.action = kMsgPackForward(@"clickDevice:", WKProxy(self), m);
        m.type = arr[i].deviceType.intValue;
        m.idfBlock = ^NSString * _Nonnull{
            return @"DeviceListCell";
        };
        m.name = arr[i].name;
        m.uuid = arr[i].identifier.UUIDString;
        m.layout.itemSize = CGSizeMake(kScreenWidth - 40, 120);
        [s.items addObject:m];
    }
    
    HomeDeviceViewModel * m = [[HomeDeviceViewModel alloc] init];
    m.action = kRunCMD(@"_show_searchView");
    m.type = 1000;
    m.img = @"添加＋";
    m.name = SWString(@"添加设备");
    m.idfBlock = ^NSString * _Nonnull{
        return @"DeviceListCell";
    };
    m.layout.itemSize = CGSizeMake(kScreenWidth - 40, 120);
    
    self.dataSources= @[s];
    [self reloadIfNeeded];
    
}

- (void)clickDevice:(HomeDeviceViewModel *)model {
    if (model.type == BleDevice_Watch) {
        ZHRunEngine(kRunCMDINFO(@"_show_deviceInfo",@{@"uuid":model.uuid}), nil);
    }else if (model.type == BleDevice_Pillow ){
        ZHRunEngine(kRunCMDINFO(@"show_PillowDeviceInfoView",@{@"uuid":model.uuid}), nil);
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
